package com.zz.common.adapter

import com.zz.common.adapter.validator.Validator
import com.zz.common.exception.TransactionException
import com.zz.dsp.idl.{DspEvent, ErrorCode}
import scala.util.Try

import com.zz.idl.IdBundle

class AdxEventAdapter(validator:Validator[DspEvent]) extends EventAdapter[Array[Byte], IdBundle] {
  override def map(in: Array[Byte]): Either[TransactionException, IdBundle] = {
    Try{
      DspEvent.parseFrom(in)
    }.toEither match {
      case Left(_) => Left(new TransactionException(ErrorCode.IllFormattedData.value))
      case Right(event) =>
        validator.validate(event).map{dspEvent =>
          IdBundle(
            ""
            , dspEvent.imei
            , dspEvent.imeiMd5
            , dspEvent.imeiSha1
            , dspEvent.imsi
            , dspEvent.imsiMd5
            , dspEvent.imsiSha1
            , dspEvent.androidId
            , dspEvent.androidIdMd5
            , dspEvent.androidIdSha1
            , dspEvent.mac
            , dspEvent.macMd5
            , dspEvent.macSha1
            , dspEvent.sn
            , dspEvent.idfa
            , dspEvent.idfv
            , dspEvent.mid
          )
        }.toEither
    }
  }


}
